package com.asurplus.mail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.asurplus.common.shiro.ShiroUtils;
import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.mail.entity.SysEmailLog;
import com.asurplus.mail.mapper.SysEmailLogMapper;
import com.asurplus.mail.service.SysEmailLogService;
import com.asurplus.mail.vo.SysEmailLogVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 邮件发送记录表 服务实现类
 * </p>
 *
 * @author lizhou
 * @since 2020-11-04
 */
@Service
public class SysEmailLogServiceImpl extends ServiceImpl<SysEmailLogMapper, SysEmailLog> implements SysEmailLogService {

    @Override
    public LayuiTableResult list(Integer page, Integer limit, SysEmailLogVO sysEmailLogVO) {
        QueryWrapper<SysEmailLogVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.del_flag", 0);
        if (StringUtils.isNotBlank(sysEmailLogVO.getSubject())) {
            queryWrapper.like("a.subject", sysEmailLogVO.getSubject());
        }
        if (null != sysEmailLogVO.getType()) {
            queryWrapper.eq("a.type", sysEmailLogVO.getType());
        }
        if (null != sysEmailLogVO.getStatus()) {
            queryWrapper.eq("a.status", sysEmailLogVO.getStatus());
        }
        if (StringUtils.isNotBlank(sysEmailLogVO.getCreateUserName())) {
            queryWrapper.like("b.name", sysEmailLogVO.getCreateUserName());
        }
        queryWrapper.orderByDesc("a.create_time");
        return new LayuiTableResult<>(this.baseMapper.listSysEmailLogVO(new Page<>(page, limit), queryWrapper));
    }

    @Override
    public void saveSysEmailLog(SysEmailLogVO sysEmailLogVO) {
        SysEmailLog sysEmailLog = new SysEmailLog();
        // 属性复制
        BeanUtil.copyProperties(sysEmailLogVO, sysEmailLog);
        sysEmailLog.setCreateUser(ShiroUtils.getSysUserId());
        this.save(sysEmailLog);
    }
}
